<?php

require_once(dirname(__FILE__) . "/../config/environment.php");
require_module("namelink");



$parameters =& $_GET;
if(!$parameters) $parameters =& $_POST;

$url = @urldecode($parameters["url"]);
$collections = @$parameters["collections"];
$logo = @$parameters["logo"];
$callback = @$parameters["callback"];
$common_names = @$parameters["common_names"];
$server_side = @$parameters["server_side"];


$collections = explode(",", $collections);
foreach($collections as &$collection) $collection = trim($collection);

$html = "";
if(!$url)
{
    $html = "";
}elseif($server_side)
{
    $html = get_remote_page($url);
    $markit = new NameTag($html);
    $html = $markit->markup_html();
    $html = NameLink::replace_tags_with_collection($html, 'NameLookup::gni');
}else
{
    $html = get_remote_page($url);
    if(!$callback) $html = NameLink::replace_relative_paths($html, $url);
    
    $to_add = '<script type="text/javascript">';
    
    if($collections) $to_add .= "var namelink_sources = ['".implode($collections, "', '")."'];";
    
    if($logo) $to_add .= "var namelink_show_logo = true;";
    else $to_add .= "var namelink_show_logo = false;";
    
    if($common_names) $to_add .= "var namelink_show_common_name = true;";
    
    $to_add .= '</script><script type="text/javascript" src="'. WEB_ROOT .'javascript/namelink.js"></script>';
    
    $html = NameLink::add_to_end($html, $to_add);
}



if($callback)
{
    $json = array("html" => $html);
    echo "$callback(".json_encode($json).")";
}else
{
    echo $html;
}






?>